﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLCONGNOLIBS.Controllers.DungDD_Controller;
using QLCONGNOLIBS.Securities.DungDD_SecurityFramework;
using QLCONGNOLIBS.Ultils;

namespace QLCONGNOAPPs.UserForms.DungDD_UserForm.userForm_DanhMucToChuc_DungDD
{
    public partial class AddUpdate_PhongBan : Form
    {
        // datafield
        private SqlConnection objConnection_g;
        private tblUser_SC objUserSc_g;
        private IDepartment_Controller_DungDD objDepartmentController_g;
        private IDepartment_Entity objEntity_g;
        private string flag_g;
        public AddUpdate_PhongBan(SqlConnection objConnection_p, tblUser_SC objUserSc_p, IDepartment_Entity objEntity_p, string flag_p)
        {
            InitializeComponent();

            objConnection_g = objConnection_p;
            objUserSc_g = objUserSc_p;
            objEntity_g = objEntity_p;
            flag_g = flag_p;
            // truyền tham số cho controller
            objDepartmentController_g = new IDepartment_Controller_DungDD(objConnection_g, objUserSc_g);
            if (flag_p.Equals("them"))
            {
                this.Text = "Thêm phòng ban";
                textEdit_MaPB.Text = "" + objEntity_g.DepartmentIdP;
                textEdit_MaPB.Enabled = false;
            }
            else
            {
                this.Text = "Cập nhật phòng ban";
                BinddingData_update(objEntity_g);
                textEdit_MaPB.Enabled = false;
            }
        }

        public void BinddingData_update(IDepartment_Entity objEntity_p)
        {
            textEdit_MaPB.Text = "" + objEntity_p.DepartmentIdP;
            textEdit_TenPB.Text = "" + objEntity_p.DepartmentName;
            memoEdit_GhiChu.Text = "" + objEntity_p.Note;
        }

        private void btn_HuyBo_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_GhiDuLieu_Click(object sender, EventArgs e)
        {
           GhiDuLieu();
        }

        public void GhiDuLieu()
        {
            errorProvider1.Clear();
            if (!Validations.IsFilled(textEdit_TenPB.Text))
            {
                errorProvider1.SetError(textEdit_TenPB, "Bạn chưa nhập Tên phòng ban");
            }
            else if (flag_g.Equals("them") && objDepartmentController_g.IsDuplicate_DepartmentName(textEdit_TenPB.Text))
            {
                errorProvider1.SetError(textEdit_TenPB, "Tên phòng ban đã tồn tại");
            }
            else
            {
                Guid maPhongBan = Guid.Parse(textEdit_MaPB.Text);
                string TenPhongBan = "" + textEdit_TenPB.Text;
                bool isDelete = false;
                if (objEntity_g.isDeleted == true)
                {
                    isDelete = true;
                }
          
                string note = "" + memoEdit_GhiChu.Text;
                IDepartment_Entity objEntity = new IDepartment_Entity(maPhongBan, TenPhongBan, isDelete, note);
                if (objDepartmentController_g.Insert_Update_Department_C(objEntity))
                {
                    this.Close();
                }
            }
        }
    }
}
